Skip to content

Conversation

@michaelcheers
Copy link
Contributor

@michaelcheers michaelcheers commented Nov 25, 2025

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows Conventional Commits
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Refactoring (no functional changes, no api changes)
  • Other... Please describe:

What is the current behavior?

The IvGenerator class uses a predictable pseudo-random number generator seeded with DateTime.Now.Ticks for generating initialization vectors (IVs) used in PDF encryption. This is a critical security vulnerability.

Security Impact:

  • An attacker who knows the approximate PDF creation time (within hours/days) can predict the IV
  • Predictable IVs allow decryption of encrypted PDF content
  • Affects anyone using the library to create encrypted PDFs

The vulnerable code:

static IvGenerator()
{
    _rc4 = new ArcfourEncryption();
    var val = DateTime.Now.Ticks;  // Predictable seed!
    // Seeds RC4 with timestamp...
}

What is the new behavior?

Replaced the predictable RC4-based PRNG with .NET's cryptographically secure RandomNumberGenerator:

public static byte[] GetIv(int len)
{
    var b = new byte[len];
    using (var rng = RandomNumberGenerator.Create())
    {
        rng.GetBytes(b);
    }
    return b;
}

This ensures:

  • ✅ Cryptographically secure random IVs
  • ✅ No predictability based on creation time
  • ✅ Proper entropy for encryption security (prevents chosen plaintext attacks)
  • ✅ Simpler, more maintainable code (removed RC4 complexity)

Does this PR introduce a breaking change?

  • Yes
  • No

The change is a drop-in replacement with the same API. All existing code continues to work unchanged, with improved security.

@gemini-code-assist
Copy link

Summary of Changes

Hello @michaelcheers, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical security vulnerability in the IvGenerator by replacing a predictable, timestamp-seeded pseudo-random number generator with .NET's cryptographically secure RandomNumberGenerator. This ensures that Initialization Vectors (IVs) used for PDF encryption are truly random and unpredictable, significantly enhancing the security of encrypted PDFs and preventing potential decryption by attackers. The change also simplifies the underlying code structure.

Highlights

  • Security Vulnerability Fix: Addressed a critical security vulnerability where the IvGenerator used a predictable pseudo-random number generator (RC4 seeded with DateTime.Now.Ticks) for PDF encryption Initialization Vectors (IVs), making them guessable.
  • Cryptographically Secure IV Generation: Replaced the insecure RC4-based PRNG with .NET's System.Security.Cryptography.RandomNumberGenerator to ensure cryptographically secure and unpredictable IVs for enhanced PDF encryption.
  • Code Simplification: Simplified the IvGenerator class by removing the ArcfourEncryption dependency and its associated static constructor logic, resulting in more maintainable and secure code.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@what-the-diff
Copy link

what-the-diff bot commented Nov 25, 2025

PR Summary

  • Enhanced Security Through Random Number Generation
    This change removes the previous method of random number generation which was based on the RC4 algorithm, a less secure technique. Instead, a feature called RandomNumberGenerator has been employed, known for its superior strength in generating numbers for cryptography, rendering our system more secure.

  • Updated Comments for Better Understanding
    To help future developers easily understand the changes, the comments explaining the generation of the random initialization vector have been revised.

  • Simplified Initialization Process
    The initialization process has been streamlined, with the removal of the prior RC4 instance setup from the static constructor. This makes our codebase cleaner and easier to manage.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses a critical security vulnerability by replacing the predictable RC4-based pseudo-random number generator with .NET's cryptographically secure RandomNumberGenerator for IV generation. The changes are well-implemented, significantly enhancing the security of PDF encryption by ensuring IVs are truly random and unpredictable. The removal of the outdated RC4 implementation also simplifies the code and improves maintainability. This is a crucial and well-executed security fix.

@VahidN VahidN merged commit 0aac749 into VahidN:master Nov 25, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants